﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Specialized;
using System.Web.Services.Protocols;
using brockredge.CLASSES;
using AjaxControlToolkit;


namespace brockredge.SERVICES
{
    /// <summary>
    /// Summary description for GetDataFromAjax
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class GetDataFromAjax : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string GetWeatherCity(string city)
        {
            return city + ":Cloudy";
        }

        [WebMethod]
        public CascadingDropDownNameValue[] getStates(string knownCategoryValues, string category)
        {

           
                List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
                testDataContext tdc = new testDataContext();
                var stateQ = from state in tdc.States
                            select state;
               
                foreach (var dr in stateQ)
                {
                    string stateName = (string)dr.state_name;
                    string stateId = (string)dr.state_id.ToString();
                    values.Add(new CascadingDropDownNameValue(stateName, stateId.ToString()));
                }
                return values.ToArray();
        }

        [WebMethod]
        public CascadingDropDownNameValue[] getCitiesByState(string knownCategoryValues, string category)
        {
            
            StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            string stateId=kv["State"];
            if (!kv.ContainsKey("State") || stateId==null)
            {
                return null;
            }
            else
            { 
                List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
                testDataContext tdc = new testDataContext();
                var cityQ = from city in tdc.Cities
                            where tdc.City_States.Any(s=>s.city_id==city.city_id && s.state_id==(Guid)kv["State"].ToUpper().ToGuid())
                            select city;
                foreach(var dr in cityQ)
                {
                    values.Add(new CascadingDropDownNameValue(dr.city_name.ToString(), dr.city_id.ToString()));
                }
                values.Add(new CascadingDropDownNameValue("Other City", "1"));
                return values.ToArray();
            }
           
        }
        [WebMethod]
        public CascadingDropDownNameValue[] getCities(string knownCategoryValues, string category)
        {           
                List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
                testDataContext tdc = new testDataContext();
                var cityQ = from city in tdc.Cities
                            select city;
                foreach (var dr in cityQ)
                {
                    values.Add(new CascadingDropDownNameValue(dr.city_name.ToString(), dr.city_id.ToString()));
                }
                return values.ToArray();
         }

        [WebMethod]
        public CascadingDropDownNameValue[] getAreas(string knownCategoryValues, string category)
        {
            StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
            string cityId = kv["City"];
            if (!kv.ContainsKey("City") || cityId == null ||cityId=="1")
            {
                return null;
            }
            else
            {
               
                List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
                testDataContext tdc = new testDataContext();
                var areaQ = from area in tdc.locations
                            where tdc.Location_Cities.Any(c => c.location_id==area.location_id && c.city_id==(Guid)cityId.ToGuid())
                            select area;
                foreach (var dr in areaQ)
                {
                    values.Add(new CascadingDropDownNameValue(dr.location_name.ToString(), dr.location_id.ToString()));
                }
                values.Add(new CascadingDropDownNameValue("Other City", "1"));
                return values.ToArray();
            }

        }
    }
}
